import type {Token} from "~/resource/types/token/token";

export const useUserStore = defineStore('userStore',
    {
        state: () => ({
            id: 0,
            userName: '',
            authenticate: '',
            authorities: [''],
            email: '',
            avatar: '',
        }),
        getters: {
            getAuthorities: (state) => state.authorities,
            getAuthenticate: (state) => state.authenticate || '',
            getUserName: (state) => state.userName || '',
            getEmail: (state) => state.email || '',
            getAvatar: (state) => state.avatar || '',
            getId: (state) => state.id,
            isAuthentication: (state) => state.id > 0 && state.authenticate,
        },
        actions: {
            setUser(loginUserInfo: Token | null) {
                if (loginUserInfo) {
                    this.id = loginUserInfo.id;
                    this.userName = loginUserInfo.userName;
                    this.authorities = loginUserInfo.authorities;
                    this.authenticate = loginUserInfo.authenticate;
                    this.avatar = loginUserInfo.avatar;
                    this.email = loginUserInfo.email;
                }
            },
            clear() {
                this.id = 0;
                this.userName = '';
                this.authenticate = '';
                this.authorities = [];
                this.avatar = ''
            }
        },
        persist: true,
    });
